package br.com.estagio.application.managedbens;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;

import org.primefaces.context.RequestContext;

import br.com.estagio.domain.entity.Pais;
import br.com.estagio.domain.service.PaisService;

@ManagedBean
public class PaisBean {

	private PaisService paisService;

	private Pais currentPais;
	
	private List<Pais> paises;
	/*-------------------------------------------------------------------
	 *				 		   CONSTRUCTORS
	 *-------------------------------------------------------------------*/
	
	public PaisBean() {
		this.currentPais = new Pais();
		this.paisService = new PaisService();
	}
	/*-------------------------------------------------------------------
	 *				 		   BEHAVIORS
	 *-------------------------------------------------------------------*/

	@PostConstruct
	public void initializeList() {
		this.fillPaises();
	}
	
	
	public void initialize(ComponentSystemEvent event) {
		if (!FacesContext.getCurrentInstance().isPostback()) {
			this.paisService = new PaisService();
			this.setCurrentPais(new Pais());			
		}
		this.fillPaises();
	}
	
	public void fillPaises() {
		this.paises = this.paisService.listPaises();
	}
	
	public void save(ActionEvent event) {
		try
		{
			this.paisService.insert(this.getCurrentPais());
			
			RequestContext.getCurrentInstance().update("tablePais");
			RequestContext.getCurrentInstance().execute("dialogAddPais.hide()");
		}
		catch ( Exception e )
		{
			e.printStackTrace();
		}			
	}
	
	public void remove (ActionEvent event) {
		try
		{
			this.paisService.removePais(this.getCurrentPais().getId());
			
			RequestContext.getCurrentInstance().update("tablePais");
			RequestContext.getCurrentInstance().execute("dialogAddPais.hide()");
		}
		catch ( Exception e )
		{
			FacesContext.getCurrentInstance().addMessage( null, new FacesMessage( FacesMessage.SEVERITY_ERROR, "Erro ao remover", "" ) );
		}
	}
	
	/*-------------------------------------------------------------------
	 *				 		   NAVIGATION
	 *-------------------------------------------------------------------*/
	public void changeToNew() {
		this.setCurrentPais(new Pais());

		RequestContext.getCurrentInstance().update("dialogFormPais");
		RequestContext.getCurrentInstance().execute("dialogAddPais.show()");
	}

	public void changeToList() {
		this.setCurrentPais(new Pais());
		
		RequestContext.getCurrentInstance().update("tablePais");
		RequestContext.getCurrentInstance().execute("dialogAddPais.hide()");
	}
	
	public void changeToEdit(Integer id) {
		this.setCurrentPais(this.paisService.findPaisById(id));
		
		RequestContext.getCurrentInstance().update("dialogFormPais");
		RequestContext.getCurrentInstance().execute("dialogAddPais.show()");
	}
	
	public void changeToDelete(Integer id) {
		this.setCurrentPais(this.paisService.findPaisById(id));
		
		RequestContext.getCurrentInstance().update("dialogFormPais");
		RequestContext.getCurrentInstance().execute("dialogAddPais.show()");
	}
	/*-------------------------------------------------------------------
	 *				 		   GETTERS AND SETTERS
	 *-------------------------------------------------------------------*/
	public PaisService getPaisService() {
		return paisService;
	}

	public void setPaisService(PaisService paisService) {
		this.paisService = paisService;
	}

	public Pais getCurrentPais() {
		return currentPais;
	}

	public void setCurrentPais(Pais currentPais) {
		this.currentPais = currentPais;
	}

	public List<Pais> getPaises() {
		return paises;
	}

	public void setPaises(List<Pais> paises) {
		this.paises = paises;
	}
}
